variable "access_key" {
  type    = string
  default = "${env("ALICLOUD_ACCESS_KEY")}"
}

variable "secret_key" {
  type    = string
  default = "${env("ALICLOUD_SECRET_KEY")}"
}

variable "ansible_vault_pass" {
  type    = string
  default = "${env("ANSIBLE_VAULT_PASS")}"
}

variable "ansible_playbook_file" {
  type    = string
  default = "${env("ANSIBLE_PLAYBOOK_FILE")}"
}

variable "ansible_playbook_dir" {
  type    = string
  default = "${env("ANSIBLE_PLAYBOOK_DIR")}"
}

variable "ansible_group_vars_dir" {
  type    = string
  default = "${env("ANSIBLE_GROUP_VARS_DIR")}"
}

variable "ansible_extra_arguments" {
  type    = string
  default = "${env("ANSIBLE_EXTRA_ARGUMENTS")}"
}

variable "ansible_extra_vars" {
  type    = string
  default = "${env("ANSIBLE_EXTRA_VARS")}"
}

variable "ssh_username" {
  type    = string
  default = "root"
}

source "alicloud-ecs" "autogenerated_1" {
  access_key           = "${var.access_key}"
  image_name           = "debian10-basic-arm-{{timestamp}}"
  instance_type        = "ecs.c6r.large"
  #security_group_id    = "sg-2ze52dgp0qaym7ummry4"
  #vpc_id               = "vpc-2zeafwou20df9llgjpf2f"
  #vswitch_id           = "vsw-2zedzi5yinwr0hzfsrxn6"
  internet_charge_type = "PayByTraffic"
  io_optimized         = "true"
  region               = "cn-hangzhou"
  secret_key           = "${var.secret_key}"
  source_image         = "debian_10_9_arm64_20G_alibase_20210706.vhd"
  ssh_pty           = true
  ssh_username      = "${var.ssh_username}"
}

build {
  sources = ["source.alicloud-ecs.autogenerated_1"]
  provisioner "shell" {
    expect_disconnect = false
    pause_before      = "20s"
    inline = [
      "sudo apt-get update",
      "sudo apt-get install -y python3 python3-pip",
      "pip3 install -U pip",
      "pip3 install netaddr pbr hvac jmespath ruamel.yaml",
      "pip3 install ansible"
    ]
  }

  provisioner "ansible-local" {
    role_paths    = [
      "../roles/init-node-debian10",
      "../roles/init-ali-k8s-centos7"
    ]
    group_vars    = "../group_vars"
    playbook_dir  = "../playbooks"
    playbook_file = "../playbooks/init-node-debian10-alicloud.yml"
    #inventory_groups = 
    #command =
    #extra_arguments = [
      #"--vault-password-file=/bin/cat",
      #"user `ansible_extra_arguments`}}",
    #  "--extra-vars deploy_env=groupclass"
    #]
  }

  provisioner "shell" {
    expect_disconnect = false
    inline = [
      "echo === System Cleanup ===",
      "sudo rm -f /root/.bash_history",
      "sudo rm -f /home/${var.ssh_username}/.bash_history",
      "sudo rm -f /var/log/wtmp",
      "sudo rm -f /var/log/btmp",
      "sudo rm -rf /var/log/installer",
      "sudo rm -rf /var/lib/cloud/instances",
      "sudo rm -rf /tmp/* /var/tmp/* /tmp/.*-unix",
      "sudo find /var/cache -type f -delete",
      "sudo find /var/log -type f | while read f; do echo -n '' | sudo tee $f > /dev/null; done;",
      "sudo find /var/lib/apt/lists -not -name lock -type f -delete",
      "sudo rm -rf /var/lib/cloud/*"
    ]
  }

}
